package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderSettingDao orderSettingDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    OrderDao orderDao;

    /**
     * 预约
     *
     * @param map
     * @return
     */
    @Override
    public Result order(Map map) throws Exception {
        String orderDate = (String) map.get("orderDate");
        //1. t_ordersetting    判断是否可预约
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);

        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getNumber() == orderSetting.getReservations()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //2. t_member          判断是否是会员
        String telephone = (String) map.get("telephone");
        String setmealId = (String) map.get("setmealId");
        Member member = memberDao.findByTelephone(telephone);
        if (member == null) {
            //不是会员，需要注册会员
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//必须要将自动增长的id查询出来 @SelectKey
        } else {
            //是会员，判断是否重复预约（同一个人同一个时间预约了同一个套餐）， t_order
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(DateUtils.parseString2Date(orderDate));
            order.setSetmealId(  Integer.parseInt((String) map.get("setmealId")) );
            order = orderDao.findByCondition(order);
            if (order != null) {
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }

        //3. t_order          添加预约信息
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//未到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        orderDao.add(order);//必须要将自动增长的id查询出来 @SelectKey

        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) {
        return orderDao.findById(id);
    }
}
